package com.iplant.wms.serviceImpl.dao.brm;


import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.mss.MSSMaterial;
import com.iplant.basic.serviceImpl.dao.mss.MSSMaterialDAO;
import com.iplant.wms.service.po.brm.BRMDistributionRange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DBBaseDAO(BRMDistributionRange.class)
public class BRMDistributionRangeDAO extends BaseDAO {

    private static BRMDistributionRangeDAO Instance = null;
    private static Logger logger = LoggerFactory.getLogger(BRMDistributionRangeDAO.class);

    private BRMDistributionRangeDAO() {
        super("");
        this.Init();
    }

    public static BRMDistributionRangeDAO getInstance() {
        if (Instance == null) {
            Instance = new BRMDistributionRangeDAO();
        }
        return Instance;
    }




    public void BRM_DeleteDistributionRange(BMSEmployee wLoginUser, int wRuleID, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wRuleID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            Map<String, Object> wDeleteParams = new HashMap<>();
            wDeleteParams.put("RuleID", wRuleID);
            this.Delete(wDeleteParams, " and ID>0 ");

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BRM_DeleteDistributionRange", ex);
        }
    }


    public BRMDistributionRange BRM_QueryDistributionRange(BMSEmployee wLoginUser,
                                               String wMaterialNo, OutResult<Integer> wErrorCode) {
        BRMDistributionRange wResult = new BRMDistributionRange();
        try {


            MSSMaterial mssMaterial =
                    MSSMaterialDAO.getInstance().SelectPO(wLoginUser, -1, wMaterialNo, wErrorCode);

            if (wErrorCode.Result != 0 || mssMaterial == null || mssMaterial.ID <= 0) {
                return wResult;
            }
            wResult = this.BRM_QueryDistributionRange(wLoginUser, mssMaterial.MaterialFullGroup, mssMaterial.ID,
                                                wErrorCode);
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BRM_QueryDistributionRange", ex);
        }
        return wResult;
    }


    public BRMDistributionRange BRM_QueryDistributionRange(BMSEmployee wLoginUser, String wMaterialGroupFullName, int wMaterialID,
                                               OutResult<Integer> wErrorCode) {
        BRMDistributionRange wResult = new BRMDistributionRange();
        try {

            List<BRMDistributionRange> wList =
                    this.BRM_QueryDistributionRangeList(wLoginUser, -1, wMaterialGroupFullName, wMaterialID, null, "",
                                                  Pagination.getMaxSize(), wErrorCode);

            if (wErrorCode.Result != 0 || wList == null || wList.size() == 0) {
                return wResult;
            }

            for (BRMDistributionRange mssDistributionRange : wList) {

                if (mssDistributionRange.MaterialID == wMaterialID) {
                    return mssDistributionRange;
                }
            }

            wList.sort((o1, o2) -> o2.MaterialGroupName.length() - o1.MaterialGroupName.length());

            wResult = wList.get(0);
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BRM_QueryDistributionRange", ex);
        }
        return wResult;
    }


    public List<BRMDistributionRange> BRM_QueryDistributionRangeList(
            BMSEmployee wLoginUser, int wMaterialGroupID, String wMaterialGroupFullName,
            int wMaterialID, String wMaterialCode, String wMaterialLike,
            Pagination wPagination, OutResult<Integer> wErrorCode) {



        Map<String, Object> wParams = new HashMap<>();

        wParams.put("MaterialGroupID", wMaterialGroupID);
        wParams.put("MaterialFullGroup", wMaterialGroupFullName);
        wParams.put("MaterialID", wMaterialID);
        wParams.put("MaterialName", wMaterialLike);

        wParams.put("MaterialCode", wMaterialCode);


        return this.SelectPOList(wLoginUser, wParams, wPagination, wErrorCode);
    }

 

}
